<template>
  <!-- 工单表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="50%"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="工单编号" prop="workOrderCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.workOrderCode"
          placeholder="请输入工单编号"
          clearable
        />

        <span v-else>{{ formData.workOrderCode }}</span>
      </el-form-item>
      <el-form-item label="工单类型" prop="workOrderName">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.workOrderName"
          placeholder="请输入工单类型"
          clearable
        />

        <span v-else>{{ formData.workOrderName }}</span>
      </el-form-item>
      <el-form-item label="外部编号" prop="externalCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.externalCode"
          placeholder="请输入外部编号"
          clearable
        />

        <span v-else>{{ formData.externalCode }}</span>
      </el-form-item>
      <el-form-item label="工厂id" prop="plantId">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.plantId"
          placeholder="请输入工厂id"
          clearable
        />

        <span v-else>{{ formData.plantId }}</span>
      </el-form-item>
      <el-form-item label="工厂编号" prop="plantCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.plantCode"
          placeholder="请输入工厂编号"
          clearable
        />

        <span v-else>{{ formData.plantCode }}</span>
      </el-form-item>
      <el-form-item label="工作中心id" prop="workCenterId">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.workCenterId"
          placeholder="请输入工作中心id"
          clearable
        />

        <span v-else>{{ formData.workCenterId }}</span>
      </el-form-item>
      <el-form-item label="工作中心编号" prop="workCenterCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.workCenterCode"
          placeholder="请输入工作中心编号"
          clearable
        />

        <span v-else>{{ formData.workCenterCode }}</span>
      </el-form-item>
      <el-form-item label="生产计划主键" prop="producePlanId">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.producePlanId"
          placeholder="请输入生产计划主键"
          clearable
        />

        <span v-else>{{ formData.producePlanId }}</span>
      </el-form-item>
      <el-form-item label="生产计划编号" prop="producePlanCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.producePlanCode"
          placeholder="请输入生产计划编号"
          clearable
        />

        <span v-else>{{ formData.producePlanCode }}</span>
      </el-form-item>
      <el-form-item label="计划年份" prop="planYear">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.planYear"
          placeholder="请输入计划年份"
          clearable
        />

        <span v-else>{{ formData.planYear }}</span>
      </el-form-item>
      <el-form-item label="计划周数" prop="planWeek">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.planWeek"
          placeholder="请输入计划周数"
          clearable
        />

        <span v-else>{{ formData.planWeek }}</span>
      </el-form-item>
      <el-form-item label="物料id" prop="materialId">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.materialId"
          placeholder="请输入物料id"
          clearable
        />

        <span v-else>{{ formData.materialId }}</span>
      </el-form-item>
      <el-form-item label="物料编号" prop="materialCode">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.materialCode"
          placeholder="请输入物料编号"
          clearable
        />

        <span v-else>{{ formData.materialCode }}</span>
      </el-form-item>
      <el-form-item label="计划数量" prop="planQuantity">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.planQuantity"
          placeholder="请输入计划数量"
          clearable
        />

        <span v-else>{{ formData.planQuantity }}</span>
      </el-form-item>
      <el-form-item label="计划批次" prop="planBatch">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.planBatch"
          placeholder="请输入计划批次"
          clearable
        />

        <span v-else>{{ formData.planBatch }}</span>
      </el-form-item>
      <el-form-item label="已完成数量" prop="planFinishQuantity">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.planFinishQuantity"
          placeholder="请输入已完成数量"
          clearable
        />

        <span v-else>{{ formData.planFinishQuantity }}</span>
      </el-form-item>
      <el-form-item label="数量单位" prop="quantityUnit">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.quantityUnit"
          placeholder="请输入数量单位"
          clearable
        />

        <span v-else>{{ formData.quantityUnit }}</span>
      </el-form-item>
      <el-form-item label="工单状态" prop="orderState">
        <el-select
          v-if="!props.isReadonly"
          v-model="formData.orderState"
          placeholder="请选择工单状态"
          clearable
        >
          <el-option
            v-for="item in dictionaryStore.filterDictList('ProduceOrderState')"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>

        <span v-else>{{
          dictionaryStore.filterDictName(
            "ProduceOrderState",
            formData.orderState
          )
        }}</span>
      </el-form-item>
      <el-form-item label="数据版本号" prop="version">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.version"
          placeholder="请输入数据版本号"
          clearable
        />

        <span v-else>{{ formData.version }}</span>
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.remarks"
          placeholder="请输入备注"
          clearable
        />

        <span v-else>{{ formData.remarks }}</span>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.createTime"
          placeholder="请输入创建时间"
          clearable
        />

        <span v-else>{{ formData.createTime }}</span>
      </el-form-item>
      <el-form-item label="创建人" prop="createdUserId">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.createdUserId"
          placeholder="请输入创建人"
          clearable
        />

        <span v-else>{{ formData.createdUserId }}</span>
      </el-form-item>
      <el-form-item label="创建人名" prop="createdUserName">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.createdUserName"
          placeholder="请输入创建人名"
          clearable
        />

        <span v-else>{{ formData.createdUserName }}</span>
      </el-form-item>
      <el-form-item label="更新时间" prop="lastUpdateTime">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.lastUpdateTime"
          placeholder="请输入更新时间"
          clearable
        />

        <span v-else>{{ formData.lastUpdateTime }}</span>
      </el-form-item>
      <el-form-item label="更新人" prop="lastUpdatedUserId">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.lastUpdatedUserId"
          placeholder="请输入更新人"
          clearable
        />

        <span v-else>{{ formData.lastUpdatedUserId }}</span>
      </el-form-item>
      <el-form-item label="更新人名" prop="lastUpdatedUserName">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.lastUpdatedUserName"
          placeholder="请输入更新人名"
          clearable
        />

        <span v-else>{{ formData.lastUpdatedUserName }}</span>
      </el-form-item>
      <el-form-item label="是否启用" prop="enabled">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.enabled"
          placeholder="请输入是否启用"
          clearable
        />

        <span v-else>{{ formData.enabled }}</span>
      </el-form-item>
      <el-form-item label="删除标识" prop="deleted">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.deleted"
          placeholder="请输入删除标识"
          clearable
        />

        <span v-else>{{ formData.deleted }}</span>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MESMESWORKORDERADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  workOrderCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  workOrderName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  externalCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  plantId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  plantCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  workCenterId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  workCenterCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  producePlanId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  producePlanCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  planYear: [],
  planWeek: [],
  materialId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  materialCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  planQuantity: [],
  planBatch: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  planFinishQuantity: [],
  quantityUnit: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  orderState: [],
  version: [],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  createTime: [],
  createdUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  createdUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  lastUpdateTime: [],
  lastUpdatedUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  lastUpdatedUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  enabled: [],
  deleted: [],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  workOrderCode: "",
  workOrderName: "",
  externalCode: "",
  plantId: "",
  plantCode: "",
  workCenterId: "",
  workCenterCode: "",
  producePlanId: "",
  producePlanCode: "",
  planYear: "",
  planWeek: "",
  materialId: "",
  materialCode: "",
  planQuantity: "",
  planBatch: "",
  planFinishQuantity: "",
  quantityUnit: "",
  orderState: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        workOrderCode,
        workOrderName,
        externalCode,
        plantId,
        plantCode,
        workCenterId,
        workCenterCode,
        producePlanId,
        producePlanCode,
        planYear,
        planWeek,
        materialId,
        materialCode,
        planQuantity,
        planBatch,
        planFinishQuantity,
        quantityUnit,
        orderState,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      } = newVal;
      Object.assign(formData, {
        id,
        workOrderCode,
        workOrderName,
        externalCode,
        plantId,
        plantCode,
        workCenterId,
        workCenterCode,
        producePlanId,
        producePlanCode,
        planYear,
        planWeek,
        materialId,
        materialCode,
        planQuantity,
        planBatch,
        planFinishQuantity,
        quantityUnit,
        orderState,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.workOrderCode = "";
  formData.workOrderName = "";
  formData.externalCode = "";
  formData.plantId = "";
  formData.plantCode = "";
  formData.workCenterId = "";
  formData.workCenterCode = "";
  formData.producePlanId = "";
  formData.producePlanCode = "";
  formData.planYear = "";
  formData.planWeek = "";
  formData.materialId = "";
  formData.materialCode = "";
  formData.planQuantity = "";
  formData.planBatch = "";
  formData.planFinishQuantity = "";
  formData.quantityUnit = "";
  formData.orderState = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.enabled = "";
  formData.deleted = "";
}

const loading = ref(false);

/** 工单保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mes/mesworkorder/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
